<?php
namespace App\Controller;

class MallAdblocksController extends AppController{

    public $_page_num = 30;
    public function initialize()
    {
        parent::initialize();
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：新增推荐国家馆
   * */
    public function add()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        $mall_adblock = $this->MallAdblocks->newEntity();
        $mall_adblock->country_id = $data['country_id'];
        $mall_adblock->title = $data['title'];
        $mall_adblock->img = empty($data['img']) ? '':$this->G->setImgUrl($data['img']);
        $mall_adblock->start_date = empty($data['start_date'])? 0:strtotime($data['start_date']);
        $mall_adblock->end_date = empty($data['end_date'])? 0:strtotime($data['end_date']);
        $mall_adblock->intro = empty($data['intro']) ? '':$data['intro'];
        $mall_adblock->is_resident = empty($data['is_resident']) ? 0:$data['is_resident'];
        $mall_adblock->order_num = empty($data['order_num']) ? 0:$data['order_num'];
        $mall_adblock->enabled = $data['enabled'] == 1 ? 1:0;
        $this->MallAdblocks->save($mall_adblock);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：修改推荐国家馆
   * */
    public function update()
    {
        $data = $this->request->getData();
        if(empty($data)){
            $this->G->error("data_error");
            return null;
        }
        $mall_adblocks = $this->MallAdblocks->findById($data['id'])->select(['id'])->first();
        if(empty($mall_adblocks)){
            $this->G->error("mall_adblock_error");
            return null;
        }
        $mall_adblock = $this->MallAdblocks->newEntity();
        $mall_adblock->id = $data['id'];
        $mall_adblock->country_id = $data['country_id'];
        $mall_adblock->title = $data['title'];
        $mall_adblock->img = empty($data['img']) ? '':$this->G->setImgUrl($data['img']);
        $mall_adblock->start_date = empty($data['start_date'])? 0:strtotime($data['start_date']);
        $mall_adblock->end_date = empty($data['end_date'])? 0:strtotime($data['end_date']);
        $mall_adblock->intro = empty($data['intro']) ? '':$data['intro'];
        $mall_adblock->is_resident = empty($data['is_resident']) ? 0:$data['is_resident'];
        $mall_adblock->order_num = empty($data['order_num']) ? 0:$data['order_num'];
        $mall_adblock->enabled = $data['enabled'] == 1 ? 1:0;
        $this->MallAdblocks->save($mall_adblock);
        $this->G->success('Success');
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：删除首页推荐国家馆
   * */
    public function delete()
    {
        $id = $this->request->getData('id');
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->MallAdblocks->deleteAll(['id'=>$id]);
        $this->G->success("Success");
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：获取推荐国家馆列表
   * */
    public function getList()
    {
        $page = empty($this->request->getData("page")) ? 1:$this->request->getData("page");
        $title = $this->request->getData("title");
        $where = [];
        if(!empty($title)){
            $where['title like'] = '%'.$title.'%';
        }
        $mall_adblocks = $this->MallAdblocks->find()
            ->where([$where])
            ->order(['order_num desc'])
            ->limit(30)
            ->page($page);
        $re = [];
        $count = $mall_adblocks->count();
        $re['pageTotal'] = ceil($count/30);
        $mall_adblocks = $mall_adblocks->toArray();
        $re['list'] = [];
        if(!empty($mall_adblocks)){
            $this->loadModel('Countries');
            $countries = $this->Countries->find()
                ->select(['id','name'])
                ->where(['id in'=>array_column($mall_adblocks, 'country_id')])
                ->toArray();
            $country = [];
            if(!empty($countries)){
                foreach ($countries as $k=>$v){
                    $country[$v['id']] = $v['name'];
                }
            }
            foreach ($mall_adblocks as $k=>$v){
                $mall_adblocks[$k]['country_title'] = empty($country[$v['country_id']]) ? '':$country[$v['country_id']];
                $mall_adblocks[$k]['start_date'] = empty($v['start_date']) ? '':date('Y-m-d H:i:s', $v['start_date']);
                $mall_adblocks[$k]['end_date'] = empty($v['end_date']) ? '':date('Y-m-d H:i:s', $v['end_date']);
                $mall_adblocks[$k]['img'] = empty($v['img']) ? '' : $this->G->getImgUrl($v['img']);
                $re['list'][] = $mall_adblocks[$k];
            }
        }
        $this->G->success("Success", $re);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：获取推荐国家馆信息
   * */
    public function getInfo()
    {
        $id = $this->request->getData('id');
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $mall_adblocks = $this->MallAdblocks->findById($id)->first();
        $this->loadModel('Countries');
        $countries = $this->Countries->find()
            ->select(['name'])
            ->where(['id'=>$mall_adblocks['country_id']])
            ->first();
        $mall_adblocks['country_title'] = empty($countries['name']) ? '':$countries['name'];
        $mall_adblocks['start_date'] = empty($mall_adblocks['start_date']) ? '':date('Y-m-d H:i:s', $mall_adblocks['start_date']);
        $mall_adblocks['end_date'] = empty($mall_adblocks['end_date']) ? '':date('Y-m-d H:i:s', $mall_adblocks['end_date']);
        $mall_adblocks['img'] = empty($mall_adblocks['img']) ? '' : $this->G->getImgUrl($mall_adblocks['img']);
        $this->G->success("Success", $mall_adblocks);
    }

    /*
   * create by phpstorm
   * author：Ming
   * date：2020.11.17
   * ps：搜索推荐国家馆列表
   * */
    public function search()
    {
        $mall_adblocks = $this->MallAdblocks->find()
            ->select(['id','title'])
            ->where(['enabled=1'])
            ->toArray();
        if(empty($mall_adblocks)){
            $this->G->success("Success", ['list'=>[]]);
            return null;
        }
        $re = [];
        foreach ($mall_adblocks as $k=>$v){
            $re['list'][] = [
                'label'=>$v['title'],
                'value'=>$v['id']
            ];
        }
        $this->G->success("Success", $re);
    }

}
?>